<?php
namespace Admin\Controller;
use Think\Controller;

class BannerController extends AdminCommonController {
	
    public function index(){
        $bs = M('Banner') -> order('sort') -> select();
		$this -> assign("bs", $bs);
        $this -> display();
    }
    
    public function addInput(){
        $this -> display();
    }
    public function add(){
        $rules = array(     
            array('title','require','请填标题'),
            array('note','require','请填写注释'),
            array('file','require','请上传图片'),
        );
		$Public = C('PUBLIC');
		$upload = new \Think\Upload();// 实例化上传类
		$upload -> rootPath = $Public;
		$upload -> savePath = 'uploads/';
		$upload -> saveName = array('uniqid','');
		$upload -> exts     = array('jpg', 'gif', 'png', 'jpeg');
		$upload -> autoSub  = true;
		$upload -> subName  = array('date','Ymd');
		$Banner = M('Banner');
		if (!$Banner->validate($rules)->create()){   
            $this->error($Banner->getError(),'',2);
        }else{
			$info = $upload->upload();//上传单文件，使用uploadOne
			if(!$info){
				// 上传错误提示错误信息    
				$this->error($upload->getError());
			} else {
				// 上传成功 获取上传文件信息    
				$img = $info['file']['savepath'].$info['file']['savename'];
				$image = new \Think\Image();
				
				if(!($image->open($Public.$img))){
					$this->error($image->getError());
				} else {
					$arr = explode('.',$img);
					$imgName = $arr[0];
					$thumb = $imgName.'_thumb.jpg';
					// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
					$image->thumb(150, 150)->save($Public.$thumb);
			        
		        	$Banner -> img = $img;
		        	$Banner -> thumb = $thumb;
					if($Banner -> add()){
						$this -> success("添加成功！", "index");
					} else {
						$this -> error("添加失败".$Banner->getError());
					}
		        }
			}
		}
    }
    public function edit(){
        $id = I("get.id");
        $Banner = M('Banner');
        $data = $Banner -> find($id);
        $this -> assign("data",$data);
		$this -> display();
    }
    
	private function upload(){
		
	}
    public function update(){
        $rules = array(     
            array('title','require','请填标题'),
            array('note','require','请填写注释'),
        );
		$Banner = M('Banner');
		if (!$Banner->validate($rules)->create()){   
            $this->error($Banner->getError(),'',2);
        }else{
        	if(!empty($_FILES['file']["tmp_name"])){
        		$Public = C('PUBLIC');
				$upload = new \Think\Upload();
				$upload -> rootPath = $Public;
				$upload -> savePath = 'uploads/';
				$upload -> saveName = array('uniqid','');
				$upload -> exts     = array('jpg', 'gif', 'png', 'jpeg');
				$upload -> autoSub  = true;
				$upload -> subName  = array('date','Ymd');
        	
				$info = $upload->upload();
				if(!$info){
					$this->error($upload->getError());
				} else {
					$img = $info['file']['savepath'].$info['file']['savename'];
					$image = new \Think\Image();
					
					if(!($image->open($Public.$img))){
						$this->error($image->getError());
					} else {
						$arr = explode('.',$img);
						$imgName = $arr[0];
						$thumb = $imgName.'_thumb.jpg';
						$image->thumb(150, 150)->save($Public.$thumb);
						//删除原有图片
						unlink($Public.$Banner->img);
    					unlink($Public.$Banner->thumb);
			        	$Banner -> img = $img;
			        	$Banner -> thumb = $thumb;
			        }
				}
			}
			$Banner -> save();
			$this -> success("修改成功！", "index");
			
		}
    }
    public function updateStatus(){
        $a_id = I("get.id");
        $Banner = M('Banner');
        $Banner -> find($a_id);
        if($Banner -> status == 0){
            $Banner -> status = 1;
        } else {
            $Banner -> status = 0;
        }
        $Banner -> save();
        $this -> success("状态修改成功");
    }
}
